#ifdef IMAM_COMMS
//#include "fm_comms.h"

#include "SDK30.H"
#include "fs.h"
#include "linklayer.h"
//#include <definekey.h>
#include <fm_proto.h>
#include <fm_hmiservice.h>
#include <fm_genpinpad.h>//imam 
#include <fm_comms.h> 
#include <cmm_ssl.h>

#define NVM_TEMP			"/HOST/NVM_TEMP"


S_FS_FILE * Hnvm;


#define nvmGetPabx() ReadSelectedFile(SELECTED_PABX)
#define  nvmAPNGet() ReadSelectedFile (SELECTED_APN)
#define nvmUserNameGet() ReadSelectedFile(SELECTED_USR)
#define nvmPasswordGet() ReadSelectedFile(SELECTED_PASS)




int writetofile(byte *buff,size_t lenbuff)
{
	int ret;
 	Hnvm=FS_open(NVM_TEMP,"w+");//create file
 	if (Hnvm!=NULL) 
		ret=FS_write(buff, 1, lenbuff, Hnvm);
	else
		_trace("write File Failed");
  FS_close(Hnvm);
  return ret;
 
}
void  DeletetempNvm(void)
{
     FS_unlink(NVM_TEMP);
}

char * ReadSelectedFile(uint8 selected)
{   
   char buff[SIZE_TEMPNVM+1];
 static  char buff2[33];
   int cnt;
   nvmtemp_t fnvm;
   	memset(buff,0x00, sizeof buff);
//	memset(buff2,0x00, sizeof buff2);
	Hnvm=FS_open(NVM_TEMP,"r+");
	
	if (Hnvm!=NULL)
		{ 

		cnt=FS_read(buff,1,SIZE_TEMPNVM,Hnvm);
		if (cnt>1)
		{ 
			   memcpy(&fnvm,buff,SIZE_TEMPNVM);
			   switch (selected)
			   {
			      case  SELECTED_PABX:
				   memcpy(buff2,fnvm.pabx,17);
				   break;
			      case SELECTED_USR:
				    memcpy(buff2,fnvm.username,32);
				    break;
				case SELECTED_APN:
				    memcpy(buff2,fnvm.APN,32);
				    break;
				case SELECTED_PASS:
				    memcpy(buff2,fnvm.password,32);
				    break;
	    		 }	
		}
	 }
  return ((char *)buff2);
  
}

#if 0
char * nvmGetPabx(void)
{
	return ReadSelectedFile(SELECTED_PABX);
}


char* nvmAPNGet(void)
{
	return ReadSelectedFile(SELECTED_APN);
}


char* nvmUserNameGet(void)
{
	return ReadSelectedFile(SELECTED_USR);
}


char* nvmPasswordGet(void)
{
	return ReadSelectedFile(SELECTED_PASS);
}

#endif

//void _trace4(const char * format,...);
//void trace4_SSL(void);

int Service_COMMS(int nSize, ST_Comms *hcomms)
{

 int retval=0;

//	_trace4("Service ID : %d,     com->handle : %d, SSL state : %s", hcomms->comms_id, hcomms->comms_Hdev, (hcomms->SSL ? "TRUE":"FALSE"));
//	_trace4("SSL profile :%d,   SSL hanlde : %d", hcomms->sslProfileHandle, hcomms->sslHandle);
//	trace4_SSL();
	switch(hcomms->comms_id)
	{
		case  SERVICE_COMMS_INIT_DEFAULT :
			InitDefaultConfig();
			break;
		case  SERVICE_COMMS_INIT 		   :
			
			//retval =
			
			writetofile(hcomms->buffsend,hcomms->lensend);
			
			//memcpy(&temp,&hcomms->ipc,sizeof(INIT_COMMS_PARAMS));
			retval=cmmInit((uint32 *)&hcomms->comms_Hdev,&hcomms->ipc, hcomms->predial);
		//	_trace_DSP2("icp", (byte *)&hcomms->ipc, sizeof(INIT_COMMS_PARAMS));
			_trace("%d.%d.%d.%d",
										hcomms->ipc.cmm.IP.ipAddrPri[0],
										hcomms->ipc.cmm.IP.ipAddrPri[1],
										hcomms->ipc.cmm.IP.ipAddrPri[2],
										hcomms->ipc.cmm.IP.ipAddrPri[3]);
		_trace("port %d",hcomms->ipc.cmm.IP.portNumPri);
			_trace("init3 %d",retval);
			break;
		case  SERVICE_COMMS_CONNECT				:
			if (hcomms->SSL)
				hcomms->retval=SslIsConnecting();
		
			break;
		case  SERVICE_COMMS_STATUS					:
			hcomms->status=cmmStatus(hcomms->comms_Hdev);
			break;
		case  SERVICE_COMMS_DISCONNECT			:
			break;
		case  SERVICE_COMMS_RECONNECT				:
			break;
		case  SERVICE_COMMS_SEND					:
			hcomms->status=cmmSend(hcomms->comms_Hdev,hcomms->buffsend, hcomms->lensend);
			break;
		case  SERVICE_COMMS_RECEIVE				:
			hcomms->status=cmmRecv(hcomms->comms_Hdev,hcomms->buffrecv, &hcomms->lenrecv,hcomms->lenMAX);
			break;
		case  SERVICE_COMMS_LISTEN					:
			hcomms->status=cmmListen(hcomms->comms_Hdev);
			break;
		case  SERVICE_COMMS_FLUSH_BUFFER			:
			cmmFlushComBuffer(hcomms->comms_Hdev,hcomms->rfu);
			break;
		case  SERVICE_COMMS_SET_DIAL_PRI			:
			cmmSetDial(hcomms->predial);
			break;
		case  SERVICE_COMMS_CLEAR_HANDLE			:
			DeletetempNvm();
			InitClearHandle();
			break;
		case  SERVICE_COMMS_KILL_THREAD			:
			killthread();
			break;
		case  SERVICE_COMMS_GET_CONNECTSTATUS2	:
			hcomms->retval=cmmGetConnectionStatus2(hcomms->comms_Hdev);
			break;
		case  SERVICE_COMMS_SSL_SEND				:
			hcomms->status=cmmSslSend(hcomms->buffsend,hcomms->lensend);
			break;
		case  SERVICE_COMMS_SSL_RECV				:
			hcomms->status=cmmSslRecv(hcomms->buffrecv, &hcomms->lenrecv,hcomms->lenMAX);
			break;
		case  SERVICE_COMMS_SSL_END				:
			hcomms->retval=cmmSslEnd();
			break;
		case  SERVICE_COMMS_GPRS_STOP				:
			hcomms->retval=cmmGPRS_Stop();
			break;
		case  SERVICE_COMMS_GPRS_INIT				:
			hcomms->retval=cmmGPRSInit(hcomms->rfu, hcomms->buffsend[0]);
			break;
		case SERVICE_COMMS_GPRS_GET_NET_STATUS://04012011
			retval=cmmGetGPRSNetworkStatus(&hcomms->status);
			return retval;
			break;
		case  SERVICE_COMMS_END					:
			hcomms->retval=cmmEnd(hcomms->comms_Hdev);
			break;
		case  SERVICE_COMMS_LAST_ERR				:
			hcomms->status=SslGetLastError();
			break;
		case  SERVICE_COMMS_SSL_SET				:
			hcomms->retval=SslSetProfile();
			break;
		case  SERVICE_COMMS_GEN_SET_PABX			:
			break;
		case  SERVICE_COMMS_GEN_SET_APN			:
			break;
		case  SERVICE_COMMS_GEN_SET_USERNAME		:
			break;
		case  SERVICE_COMMS_GEN_SET_PASSWORD		:
			break;
		case  SERVICE_COMMS_SSL_PRINT_PROFILE		:
			PrintSSLProfile();
			break;
	
		
		default:
			return STOP;	
	}
	return FCT_OK;
  
}


#endif
